import axios from "axios"
// import { Modal } from "antd"
// import useStorage from "@/hooks/useStorage"
//组件外使用router编程式导航的方法
// import { useNavigate } from "react-router-dom";

// let navigate = useNavigate()
let http = axios.create({
    baseURL: '/api',
    timeout: 5000,
    withCredentials: true
})


//请求拦截器

http.interceptors.request.use((config:any) => {

    // let [token,methods] = useStorage('qf-token')
    let token = JSON.parse(localStorage.getItem('qf-token') as string)
    
    config.headers!.authorization = token as string
    
    return config
})

//响应拦截

http.interceptors.response.use((config:any) => {
    // //判断后台返回的状态
    // let { state, code } = config.data;
    // if (!state) {
    //     Modal.error({
    //         title: "错误提示",
    //         content: config.data.msg,
    //         centered: true
    //     })
    // }

    // //如果状态码是10022和1004 说明登入过期 要重新跳转到登入页
    // if (code === "10022" || code === "1004") {
    //     window.location.reload()
    //     let [token,methods] = useStorage('qf-token')
    //     let [userInfo,method] = useStorage('qf-userInfo')
    //     methods.rm()
    //     method.rm()
    //     // navigate('/login')
        
    // }
    // window.location.reload()
    return config
})

export default http